﻿package com.guavestudios.fcfg.readers {
	import com.guavestudios.fcfg.Fcfg;
	import com.guavestudios.fcfg.readers.ISettingReader;
	
	/**
	 * ...
	 * @author maze | guavestudios.com
	 */
	public class BasicSettingReader implements ISettingReader{
		
		protected var _fcfg:Fcfg;
		protected var _idMap:Object;
		protected var _idMapRev:Object;
		protected var _settings:Object = { };
		protected var _idMapReg:RegExp =/^(.*)(?:-\>(.*)$)/;
		protected var _priority:int = 10;
		protected var _order:int;
		
		public function BasicSettingReader() {
			
		}
		
		/* INTERFACE com.guavestudios.fcfg.readers.ISettingReader */
		public function set fcfg(fcfg:Fcfg):void { _fcfg = fcfg; notifyFcfg(); }
		
		
		public function get fcfg():Fcfg{ return _fcfg;}
		
		public function getSetting(id:String):*{
			return _settings[id];
		}
		
		public function hasSetting(id:String):Boolean{
			return _settings.hasOwnProperty(id);
		}
		public function setSetting(id:String, value:*):void {
			_settings[id] = value;
			if (_fcfg != null)
				_fcfg.updateSetting(id);
		}
		public function removeSetting(id:String):void {
			delete _settings[id];
			if (_fcfg != null)
				_fcfg.updateSetting(id);
		}
		protected function notifyFcfg():void {
			if (_fcfg!=null)
			for (var i:String in _settings) {
				_fcfg.updateSetting(i);
			}
		}
		
		public function load(...args):void {
			
		}
		
		public function mapId(key1:String):String {
			if (_idMap!=null)
			if (_idMap.hasOwnProperty(key1))
				return _idMap[key1];
			return key1;
		}
		public function mapIdRev(key2:String):String {
			if (_idMapRev!=null)
			if (_idMapRev.hasOwnProperty(key2))
				return _idMapRev[key2];
			return key2;
		}
		public function setMapId(key1:String, key2:String):void {
			if (_idMap == null) {
				_idMap = { }; _idMapRev = { };
			}
			_idMap[key1] = key2;
			_idMapRev[key2] = key1;
		}
		public function set idMap(obj:Object):void {
			for (var i:String in obj) {
				setMapId(i, obj[i]);
			}
		}
		
		/* INTERFACE com.guavestudios.fcfg.readers.ISettingReader */
		
		public function get priority():int{
			return _priority;
		}
		
		public function set priority(p:int):void{
			_priority = p;
		}
		
		public function get order():int { return _order; }
		
		public function set order(value:int):void {
			_order = value;
		}
		
	}

}